package io.mvvm.ws.chain;

import io.mvvm.ws.entity.RequestInfo;
import io.mvvm.ws.entity.ResponseInfo;
import io.mvvm.ws.enumerate.ChainMessageTypeEnum;
import io.mvvm.ws.enumerate.SendMessageTypeEnum;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: Netty
 * @description: 订阅消息处理
 * @author: 潘
 * @create: 2021-04-28 19:45
 **/
@Component
public final class SubscribeMessageHandler extends MessageHandler {

    private static final Logger logger = LoggerFactory.getLogger(SubscribeMessageHandler.class);

    @Override
    protected boolean isMatched(RequestInfo request) {
        return ChainMessageTypeEnum.SUBSCRIBE.getAction().equals(request.getAction());
    }

    @Override
    protected ResponseInfo handler(RequestInfo request, ChannelHandlerContext ctx) {

        logger.info("执行订阅消息处理器...");

        Map<String, Object> params = new HashMap<>();
        params.put("client_msg", request.getParams().get("msg"));
        return ResponseInfo.builder()
                .msg("处理订阅消息")
                .action(SendMessageTypeEnum.SUBSCRIBE.getAction())
                .params(params)
                .build();
    }
}
